package com.tfswx.fljdmxyy.service.impl;

import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.service.WjfwService;
import com.tfswx.storage.wjfw.autoconfigure.dto.relation.RelationConfirmFilesInputDTO;
import com.tfswx.storage.wjfw.autoconfigure.dto.relation.RelationDeleteInputDTO;
import com.tfswx.storage.wjfw.autoconfigure.dto.relation.RelationFileDeleteInputDTO;
import com.tfswx.storage.wjfw.autoconfigure.service.StorageRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 文件服务实现类
 *
 * @Author 陈文佳
 * @Date 2023/03/24
 */
@Service
public class WjfwServiceImpl implements WjfwService {
    @Resource
    private StorageRelationService storageRelationService;

    @Override
    public void confirmFile(String zylxbm, String zybm, String wjbh) {
        RelationConfirmFilesInputDTO relationConfirmFilesInputDTO = new RelationConfirmFilesInputDTO();
        relationConfirmFilesInputDTO.setMkbm(SystemConstant.MKBM);
        relationConfirmFilesInputDTO.setZylxbm(zylxbm);
        relationConfirmFilesInputDTO.setZybm(zybm);
        List<String> whbhList = new ArrayList<>();
        whbhList.add(wjbh);
        relationConfirmFilesInputDTO.setWjbhs(whbhList);
        try {
            storageRelationService.confirm(relationConfirmFilesInputDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(BusinessCodeEnum.WJWZD);
        }

    }

    @Override
    public void deleteFile(String zylxbm, String zybm) {
        RelationDeleteInputDTO relationDeleteInputDTO = new RelationDeleteInputDTO();
        relationDeleteInputDTO.setMkbm(SystemConstant.MKBM);
        relationDeleteInputDTO.setZylxbm(zylxbm);
        relationDeleteInputDTO.setZybm(zybm);
        try {
            storageRelationService.delete(relationDeleteInputDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(BusinessCodeEnum.WJWZD);
        }
    }

    @Override
    public void deleteFile(String wjbh) {
        RelationFileDeleteInputDTO relationDeleteInputDTO = new RelationFileDeleteInputDTO();
        relationDeleteInputDTO.setWjbhs(Collections.singletonList(wjbh));
        try {
            storageRelationService.delete(relationDeleteInputDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(BusinessCodeEnum.WJWZD);
        }
    }
}
